﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AccountProxy = ChatForum.Client.AccountServiceProxy;
using ForumProxy = ChatForum.Client.ForumServiceProxy;

namespace ChatForum.Client.Admin
{
    public partial class User : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
			if (!IsPostBack)
				LoadUserGrid();
        }

		private void LoadUserGrid()
		{
			var v = ServiceHelper.Instance.AccountService.GetAllUser();
            Session["AllUser"] = v;
			userListGrid.DataSource = v;
			userListGrid.DataBind();
		}
		protected void OnBlockUser(object sender, EventArgs e)
		{
			PageHelper.GetSelectedObjectFromGrid<AccountProxy.User>(this, "userListGrid", "AllUser", "userSelector").ForEach(u =>
			{
				ServiceHelper.Instance.AccountService.BlockUser(u);
			});
			LoadUserGrid();
		}
		protected void OnUnblockUser(object sender, EventArgs e)
		{
			PageHelper.GetSelectedObjectFromGrid<AccountProxy.User>(this, "userListGrid", "AllUser", "userSelector").ForEach(u =>
			{
				ServiceHelper.Instance.AccountService.UnblockUser(u);
			});
			LoadUserGrid();
		}
		protected void OnSearchUser(object sender, EventArgs e)
		{

		}
		protected void userListGrid_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				AccountProxy.User u = (AccountProxy.User)e.Row.DataItem;
                e.Row.CssClass = u.Status ? "unblockedUserStyle" : "blockedUserStyle";
                e.Row.Enabled = u.UserId != User.Identity.Name;
			}
		}
		protected void OnEmailRequest(object sender, EventArgs e)
		{
			PageHelper.GetSelectedObjectFromGrid<AccountProxy.User>(this, "userListGrid", "AllUser", "userSelector").ForEach(u =>
			{
				ServiceHelper.Instance.AccountService.SendMessage(u, txtMailSubject.Text, txtMailBody.Text);
			});
			LoadUserGrid();
		}
        protected void FilterUser(object sender, EventArgs e)
        {
            userListGrid.DataSource = ServiceHelper.Instance.AccountService.GetAllUser().Where(u => u.UserId.Contains(txtSearchUser.Text));
            userListGrid.DataBind();
        }
    }
}